package com.iuhao.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ddk.en.db.mapper.ddkonline.UserMapper;
import com.ddk.en.db.model.ddkonline.User;
import com.iuhao.web.core.exception.O2OServerException;
import com.iuhao.web.core.response.O2OJSON;
import com.iuhao.web.core.utils.CusAccessObjectUtil;
import com.iuhao.web.core.utils.HttpRequestUtils;
import com.iuhao.web.model.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yanhao on 2017/12/15.
 */
@Api(tags = "UserController", description = "用户个人中心接口")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户个人中心
     */

    @RequestMapping(value = "/{accountId}/getMyInfo", method = RequestMethod.GET)
    @ApiOperation(value = "我的个人信息")
    public O2OJSON checkNum(@PathVariable long accountId , HttpServletRequest request){
        O2OJSON o2OJSON = new O2OJSON();
        JSONObject object = new JSONObject();
        try {
            //
            User user = userMapper.getUserInfoById(accountId);
            if (user==null){
                o2OJSON.setSuccessAndNullData();
                return o2OJSON;
            }
           //object.put("user",user);
            o2OJSON.setData(user);
        } catch (Exception e) {
            exception(e,o2OJSON);
        }
        return o2OJSON;
    }

    @RequestMapping(value = "/updateMyInfo", method = RequestMethod.POST)
    @ApiOperation(value = "修改基本信息")
    public O2OJSON checkNum(@RequestBody UpdateMyInfoParam param , HttpServletRequest request){
        O2OJSON json = new O2OJSON();
        try {
            User user = userMapper.getUserInfoById(param.getAccountId());
            if (user==null){
                json.setSuccessAndNullData();
                return json;
            }
            user.setAddress(param.getAddress());
            user.setEmail(param.getEmail());
            userMapper.updateUserInfoById(user);
            json.success();
        } catch (Exception e) {
            exception(e,json);
        }
        return json;
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录接口")
    public O2OJSON checkNum(@RequestBody LoginParam param , HttpServletRequest request){
        O2OJSON json = new O2OJSON();
        try {
            User user=userMapper.getUserInfoByPhone(param.getLoginName());
            if (user==null || !user.getPassword().equals(param.getPassword())){
                throw  new O2OServerException("用户名或密码有误,请重新输入");
            }
            json.setData(user);
        } catch (Exception e) {
            exception(e,json);
        }
        return json;
    }

    @RequestMapping(value = "/editPassword", method = RequestMethod.POST)
    @ApiOperation(value = "修改密码接口")
    public O2OJSON editPassword(@RequestBody EditPasswordParam param , HttpServletRequest request){
        O2OJSON json = new O2OJSON();
        try {
            User user=userMapper.getUserInfoById(param.getAccountId());
            if (user!=null && user.getPassword().equals(param.getPassword())){
                user.setAccountId(user.getAccountId());
                user.setPassword(param.getRepassword());
                userMapper.updateUserInfoById(user);
                json.setData(user);

            }else{
                throw  new O2OServerException("用户名或密码有误,请重新输入");
            }
        } catch (Exception e) {
            exception(e,json);
        }
        return json;
    }


}
